
import * as THREE from "three"
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"

import { MyThree } from "../utils/init_three"
import { City } from "../utils/City"
import * as TWEEN from "@tweenjs/tween.js"

let myThree
let canvas: HTMLCanvasElement, renderer: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.PerspectiveCamera

export const init = () => {
    canvas = document.getElementById("myCanvas") as HTMLCanvasElement
    // myThree = new MyThree(canvas)
    // let city = new City(myThree)

    // function start() {
    //     myThree.animation()
    //     city.start()
    //     requestAnimationFrame(start)
    // }
    // start()

    const scene = new THREE.Scene()
    const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000)
    camera.position.set(500, 500, 500)
    camera.lookAt(new THREE.Vector3(0, 0, 0))
    const renderer = new THREE.WebGLRenderer({ canvas })
    renderer.setSize(window.innerWidth, window.innerHeight)
    renderer.setClearColor(0x000)

    const light = new THREE.PointLight(0xffffff)
    light.position.set(300, 500, 300)
    scene.add(light)
    const controls = new OrbitControls(camera, renderer.domElement)
    controls.maxDistance = 1500
    const city = new City(scene, camera, renderer,controls)
    const clock = new THREE.Clock()

    const axesHelper = new THREE.AxesHelper(500);
    // scene.add(axesHelper);

    function start() {
        //！！！city在前,不然无法旋转相机视角
        city.start(clock.getDelta())
        controls.update()
        // TWEEN.update()
        renderer.render(scene, camera)
        requestAnimationFrame(start)
    }
    start()
    window.addEventListener('resize', () => {
        // 更新宽高比
        camera.aspect = window.innerWidth / window.innerHeight
        // 更新相机的投影矩阵
        camera.updateProjectionMatrix()

        renderer.setSize(window.innerWidth, window.innerHeight)
        // 设置像素比
        renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
    })
}